package com.itfreer.file.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.OSSObject;
import com.itfreer.file.FileSegmentInfo;
import com.itfreer.file.service.IObjectDownloadService;

/**
 * 定义对象下载接口
 */
@Component
public class OssObjectDownload implements IObjectDownloadService, BeanFactoryAware {

	protected static BeanFactory beanFactory;

	@Override
	public void setBeanFactory(BeanFactory bf) throws BeansException {
		beanFactory = bf;
	}
	
	private AliyunOssPara aliyunOssPara;
	
	private AliyunOssPara getPara(){
		if(aliyunOssPara==null){
			aliyunOssPara = (AliyunOssPara) beanFactory.getBean("aliyunOssPara");
		}
		return aliyunOssPara;
	}
	
	private OSSClient getClient(){
		OSSClient ossClient = new OSSClient(getPara().getEndpoint(), getPara().getAccessKeyId(), getPara().getAccessKeySecret());
		return ossClient;
	}
	
	/**
	 * 获取对象
	 * @param bucketName	存储块名称
	 * @param objectKey		对象名称主键
	 */
	public byte[] getObject(String bucketName, String objectKey){
		// 创建OSSClient实例
		OSSClient ossClient = getClient();
				
		OSSObject ossObject = ossClient.getObject(bucketName, objectKey);
		InputStream inputStearm = ossObject.getObjectContent();
		ossClient.shutdown();
		
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
        byte[] buff = new byte[1024];  
        int rc = 0;  
        try {
			while ((rc = inputStearm.read(buff, 0, 1024)) > 0) {  
			    swapStream.write(buff, 0, rc);  
			}
		} catch (IOException e) {
			e.printStackTrace();
		}  
        byte[] in2b = swapStream.toByteArray();  
        return in2b;
	}
	
	/**
	 * 断点续传下载
	 * @param bucketName		存储块名称
	 * @param objectKey			对象存储key
	 * @param downloadFile		本地文件
	 * @param partSize			分片大小
	 */
	public void downloadFile(String bucketName, String objectKey, String downloadFile, Integer partSize){
		// 创建OSSClient实例
		OSSClient ossClient = getClient();
		// 下载请求，10个任务并发下载，启动断点续传
		DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, objectKey);
		downloadFileRequest.setDownloadFile(downloadFile);
//		if(taskNum!=null){
//			downloadFileRequest.setTaskNum(taskNum);
//		}
		if(partSize!=null){
			downloadFileRequest.setPartSize(partSize);
		}
		downloadFileRequest.setEnableCheckpoint(true);
		// 下载文件
		DownloadFileResult downloadRes;
		try {
			downloadRes = ossClient.downloadFile(downloadFileRequest);
			// 下载成功时，会返回文件的元信息
			downloadRes.getObjectMetadata();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		// 关闭client
		ossClient.shutdown();
	}

	@Override
	public FileSegmentInfo downloadFile(String bucketName, String objectKey, FileSegmentInfo segmentInfo) {
		// TODO Auto-generated method stub
		return null;
	}
}
